package com.youxin.cpcenter.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.youxin.busfoundation.bean.login.UserInfo;
import com.youxin.busfoundation.common.LoginAuth;
import com.youxin.cpcenter.bean.RequestEntConsumeBean;
import com.youxin.cpcenter.dao.TbEntConsumeMapper;
import com.youxin.cpcenter.model.TbEntConsume;
import com.youxin.cpcenter.service.EntConsumeService;
import com.youxin.foundation.db.utils.KeyGenerationUtils;
import com.youxin.foundation.utils.DateUtil;

/**
 * 
 * @ClassName: EntConsumeServiceImpl 
 * @Description: 企业消费记录 
 * @author yingde.cao
 * @date 2017年12月15日 下午7:18:19 
 *
 */
@Service("entConsumeService")
public class EntConsumeServiceImpl extends BaseServiceImpl implements EntConsumeService {
	@Autowired
	private TbEntConsumeMapper tbEntConsumeMapper;
	/**
	 * 保存 企业消费记录 
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public void insertEntConsume(TbEntConsume model) {
		int rowEffected = 0;
		try {
			//UserInfo userInfo = LoginAuth.getUserInfo();
			model.setId(KeyGenerationUtils.generateKey());
//			model.setCompanyUid(userInfo.getCompanyUid());
//			model.setCompanyFullName(userInfo.getCompanyName());
//			model.setCallerUserId(userInfo.getId());
//			model.setCallerUserName(userInfo.getRealname());
//			model.setEntMemberId(userInfo.getEntMemberId());
			model.setAddTime(System.currentTimeMillis());
			tbEntConsumeMapper.insertSelective(model);
			if (rowEffected != 1) {
				//throw new BusinessException("保存企业消费记录异常");
			}
		} catch (Exception e) {
			logger.error("insertEntConsume:",e);
			//throw new BusinessException("insertEntConsume异常"+e);
		}
	}
	/**
	 * 查询 消费记录
	 */
	public List<TbEntConsume> selectByPrimaryKey(RequestEntConsumeBean bean) {
		UserInfo userInfo = LoginAuth.getUserInfo();
		if(userInfo.isAdmin())
		{
			bean.setEntIdentifyId(userInfo.getEntIdentifyId());
		}else {
			bean.setEntMemberId(userInfo.getEntMemberId());
			bean.setCallerUserId(userInfo.getId());
		}
		int timeType = bean.getTimeType();
		switch (timeType) {
		//当天
		case 1:
			bean.setStartTime(DateUtil.getDayStartTimeString(new Date()));
			bean.setEndTime(DateUtil.getDayEndTimeString(new Date()));
			break;
			//本周
		case 2:
			bean.setStartTime(DateUtil.getFormatString(DateUtil.getBeginDayOfWeek()));
			bean.setEndTime(DateUtil.getFormatString(DateUtil.getEndDayOfWeek()));	
			break;
			//本月
		case 3:
			bean.setStartTime(DateUtil.getFormatString(DateUtil.getBeginDayOfWeek()));
			bean.setEndTime(DateUtil.getFormatString(DateUtil.getEndDayOfMonth()));
			break;
			//其它
		default:
			break;
		}
		
		List<TbEntConsume> list = tbEntConsumeMapper.selectByPrimaryParam(bean);
		for (TbEntConsume tbEntConsume : list) {
			if(tbEntConsume.getConsumeType().equals(1) && tbEntConsume.getCountNum() != null){
				int seconds = Integer.parseInt(tbEntConsume.getCountNum());
				int minutes = seconds / 60;   
		        int remainingSeconds = seconds % 60;
		        if(remainingSeconds >0) {
		        	minutes++;
		        }
				tbEntConsume.setCountNum(minutes+"");
			}
		}
		bean.setResults(list);
		return list;
	}
}

